package com.asset.dispoal.verdict.service.common;

import com.asset.dispoal.verdict.domain.db.entity.GbJudgmentBookItem;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookItemDetailMapper;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookItemMapper;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-06
 */
@Repository
public class DeleteJudgmentCommand implements BaseCommand<String, Result> {

    @Autowired
    private GbJudgmentBookMapper judgmentBookMapper;
    @Autowired
    private GbJudgmentBookItemMapper gbJudgmentBookItemMapper;
    @Autowired
    private GbJudgmentBookItemDetailMapper gbJudgmentBookItemDetailMapper;

    @Override
    @Transactional
    public Result execute(String judgmentId) {
        List<GbJudgmentBookItem> judgmentBookItems = gbJudgmentBookItemMapper.getBookItemByJudgmentId(judgmentId);
        int i = judgmentBookMapper.deleteById(judgmentId);
        gbJudgmentBookItemMapper.deleteByBookId(judgmentId);
        for (GbJudgmentBookItem bookItem : judgmentBookItems) {
            gbJudgmentBookItemDetailMapper.delDetailByItemId(bookItem.getId());
        }
        return i <= 0 ? Result.faild("删除数据失败") : Result.ok();
    }
}
